//
//  ModifyPasswordVC.m
//  优学荟管理系统
//
//  Created by 星道 on 17/9/4.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ModifyPasswordVC.h"

@interface ModifyPasswordVC ()
{
    IBOutlet UITextField *_phone;
    IBOutlet UILabel *_phonePH;
    IBOutlet UITextField *_oldPwd;
    IBOutlet UILabel *_oldPwdPH;
    IBOutlet UITextField *_newPwd;
    IBOutlet UILabel *_newPwdPH;
    IBOutlet UITextField *_surePwd;
    IBOutlet UIButton *_sure;
    IBOutlet UILabel *_surePH;
}
@end

@implementation ModifyPasswordVC

#pragma makr - 生命周期
- (void)viewDidLoad {

    [super viewDidLoad];

    self.title = @"修改密码";

    _phone.text = [AccountTool shareAccountTool].currentAccount.username;

    [self ButtonSelectStatus:_sure imageName:@"btn_next_default"];
}





#pragma mark - 方法
/** 警示语句的展示 */
- (void)ShowPlaceHolder:(UITextField *)textField placeHolder:(UILabel *)placeHolder complete:(void (^)(BOOL is))complete {

    // 手机号码
    if (textField == _phone) {

        complete ? complete (![LLTools valiMobile:_phone.text]):nil;
        [UIView animateWithDuration:0.3 animations:^{
            _phonePH.alpha = [LLTools valiMobile:_phone.text] ? 1:0;
        }];

    }

    if (textField == _oldPwd) {
        // 旧密码
        BOOL is = [_oldPwd.text isEqualToString:[AccountTool shareAccountTool].currentAccount.password];
        complete ? complete (is):nil;
        [UIView animateWithDuration:0.3 animations:^{
            _oldPwdPH.alpha = is ? 0:1;
        }];
    }

    if (textField == _newPwd) {

        // 新密码
        if (_newPwd.text.length < 6 || _newPwd.text.length > 16 ) {

            complete ? complete (NO):nil;
            [UIView animateWithDuration:0.3 animations:^{
                _newPwdPH.alpha = 1;
            }];
        } else {

            complete ? complete (YES):nil;
            [UIView animateWithDuration:0.3 animations:^{
                _newPwdPH.alpha = 0;
            }];
        }
    }

    if (textField == _surePwd) {

        // 确认密码
        if (![_surePwd.text isEqualToString:_newPwd.text]) {
            complete ? complete (NO):nil;
            [UIView animateWithDuration:0.3 animations:^{
                _surePH.alpha = 1;
            }];
        } else {

            complete ? complete (YES):nil;
            [UIView animateWithDuration:0.3 animations:^{
                _surePH.alpha = 0;
            }];
        }
    }
}


/** 设置按钮的状态 */
- (void)ButtonSelectStatus:(UIButton *)button imageName:(NSString *)imageName {

    [button setBackgroundImage:[UIImage imageNamed:imageName] forState:(UIControlStateNormal)];
    [button setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [button setBackgroundImage:[LLTools createImageWithColor:ColorRGB(240, 240, 240)] forState:(UIControlStateSelected)];
    [button setTitleColor:[UIColor lightGrayColor] forState:(UIControlStateSelected)];
}

/** 更改按钮值 */
- (void)ButtonSelect:(UIButton *)button isEnable:(BOOL)isEnable text:(NSString *)text {

    button.userInteractionEnabled = isEnable;
    button.selected = !isEnable;
    [button setTitle:text forState:UIControlStateNormal];
    if (!button.selected) {

        button.layer.cornerRadius = 0;
        button.layer.masksToBounds = NO;

    } else {

        button.layer.cornerRadius = button == _sure ? 10.f:15.f;
        button.layer.masksToBounds = YES;
    }
}

- (void)Pop {

    [self.navigationController popToRootViewControllerAnimated:YES];
}




#pragma mark - 修改密码
- (IBAction)Sure:(UIButton *)sender {

    __block BOOL is = NO;
    // 条件判断
    [self ShowPlaceHolder:_phone placeHolder:_phonePH complete:^(BOOL first) {
        is = first;
    }];

    // 手机号码
    [self ShowPlaceHolder:_newPwd placeHolder:_newPwdPH complete:^(BOOL second) {
        // 新密码
        is = is ? second:is;
    }];

    [self ShowPlaceHolder:_surePwd placeHolder:_surePH complete:^(BOOL three) {
        // 确认密码
        is = is ? three:is;
    }];




    if (is) {

        [self ShowPlaceHolder:_oldPwd placeHolder:_oldPwdPH complete:^(BOOL four) {

            if (four) {

                // 开始请求
                // 修改的请求
                [self ButtonSelect:sender isEnable:NO text:@"请稍候..."];
                // NSString *urlStr = [NSString stringWithFormat:@"%@?action=%@&uid=%@&pwd=%@",LLBaseURLTeacher(@"user.do"),@"setPwd",[AccountTool shareAccountTool].currentAccount.uid,_surePwd.text];
                NSDictionary *params = @{
                                         @"action":@"setPwd",
                                         @"uid":[AccountTool shareAccountTool].currentAccount.uid,
                                         @"pwd":_surePwd.text
                                         };
                [NetManager Post:LLBaseURLTeacher(@"user.do") Auth:NO Params:params Success:^(id Obj) {

                    // 说是空值，但Obj还是有值的，只能不做判断了

                    [HUD showSuccess:@"修改密码成功"];
                    // 通知上级界面直接去到登录界面
                    self.back ? self.back ():nil;
                    [self performSelector:@selector(Pop) withObject:nil afterDelay:1.5];

                } Fail:^(NSError *Error) {

                    [HUD showError:NetErr];
                    [self ButtonSelect:sender isEnable:YES text:@"修改"];
                }];

            }

        }];
    }
}
@end
